Oa.controller('OaCtrl',function($rootScope,$scope,$interval,$location,$http,$stateParams){
	$scope.TableTitle = "首页";

	$scope.$on('$stateChangeSuccess', function(){
		if($location.path() === "/mainOa"){
			$scope.TableTitle = "首页";
		}else if($location.path() === "/meeting"){
			$scope.TableTitle = "会议管理";
		}else if($location.path() === "/dealApply"){
			$scope.TableTitle = "审核申请单";
		}else if($location.path() === "/myApply"){
			$scope.TableTitle = "我的申请记录";
		}else if($location.path() === "/showEssay"){
			$scope.TableTitle = "团建活动-查看文章";
		}else if($location.path() === "/staffManage"){
			$scope.TableTitle = "员工信息管理";
		}else if($location.path() === "/suggestionleaderbox"){
			$scope.TableTitle = "意见箱记录";
		}else if($location.path() === "/suggestionbox"){
			$scope.TableTitle = "意见箱";
		}else if($location.path() === "/teamactivity"){
			$scope.TableTitle = "团建活动";
		}else if($location.path() === "/essayWrite"){
			$scope.TableTitle = "团建活动-编写文章";
		}else if($location.path() === "/fileAll"){
			$scope.TableTitle = "文件管理-全部";
		}else if($location.path() === "/fileDoc"){
			$scope.TableTitle = "文件管理-文档";
		}else if($location.path() === "/fileJpg"){
			$scope.TableTitle = "文件管理-图片";
		}else if($location.path() === "/fileMp4"){
			$scope.TableTitle = "文件管理-视频";
		}
	});

	$scope.admin = false;
//	var e = "admin@qq.ccom";
//	if (sessionStorage.email==e) {
//		console.log("admin")
//		$scope.admin = true;
//	}

	//更改头像
	$scope.changePhoto = function(){
		$("#headphoto").click();
	}
	$scope.uploadHeadPhoto = function(){
	  var formData = new FormData($( "#headphotoForm" )[0]);
	  formData.append("userId",sessionStorage.userid);
      var xhr = new XMLHttpRequest();
      xhr.onload=function(event)
      {
        if  (( xhr.status >= 200 && xhr.status < 300) || xhr.status == 304)   //上传成功
        {
        	$scope.getUserInfo(sessionStorage.userid);
        }
        else
        {
          Notify("更改头像失败","top-right");
        }
      };

      xhr.open("post", updateHeadphotoById_url ,true);
      xhr.send(formData);
	}

	//退出系统
	$scope.loginOut = function(){
		sessionStorage.clear();
		location.href = "./login.html";
	}

	if(!sessionStorage.userid){
		location.href = "./login.html";
	}

	$scope.getUserInfo = function(id){
		$http.get(getUserById_url,{
			params:{
				userId:id
			}
		})
		.success(function(res){
			if (res.code==200) {
				$rootScope.user = res.data;
				if(res.data.birth!=null){
					var year = parseInt(res.data.birth.substr(0,4));
					var date = new Date();
					var dateyear = date.getFullYear();
					$rootScope.user.age = dateyear - year;
				}
				$scope.admin = (res.data.email=="admin@qq.com")?true:false;
			}
		})
		.error(function(){

		})
	}

	$scope.getUserInfo(sessionStorage.userid);


	$scope.getMyNews = function(){
		$http.get(getApplyByReceiver_url,{
			params:{
				receiver:sessionStorage.email
			}
		})
		.success(function(res){
			if (res.code==200) {
				$scope.news = [];
				for (var i = 0; i < res.data.length; i++) {
					if (!res.data[i].dealStatus) {

						var a = new Date(res.data[i].sendDate);
	          res.data[i].sendDate = a.toLocaleString();
						$scope.news.push(res.data[i]);
					}
				}
				$scope.newsNum = $scope.news.length;
			}else {
				Notify(res.data,"top-right");
			}
		})
		.error(function(){
			Notify("获取失败，请稍后重试","top-right");
		});
	}

	$scope.getMyNews();
	$interval($scope.getMyNews,300000);
});
